from mcp.server.fastmcp import FastMCP

# 创建MCP服务实例
mcp = FastMCP("Math Server")

@mcp.tool()
def add(a: float, b: float) -> float:
    """加法运算"""
    return a + b

@mcp.tool()
def subtract(a: float, b: float) -> float:
    """减法运算"""
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """乘法运算"""
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> float:
    """除法运算"""
    if b == 0:
        raise ValueError("除数不能为零")
    return a / b

@mcp.tool()
def factors(n: int) -> list:
    """求一个数的所有公因子"""
    if n <= 0:
        raise ValueError("输入必须为正整数")
    return [i for i in range(1, n+1) if n % i == 0]

@mcp.tool()
def factorial(n: int) -> int:
    """求一个数的连乘(阶乘)"""
    if n < 0:
        raise ValueError("输入不能为负数")
    return 1 if n == 0 else n * factorial(n-1)

if __name__ == "__main__":
    mcp.run(transport="stdio")